package com.hzf.wxplatform.web.controller.manager.designer;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.util.Base64;
import com.github.wenhao.geohash.GeoHash;
import com.github.wenhao.geohash.GeoSearch;
import com.github.wenhao.geohash.domain.GeoRange;
import com.google.gson.Gson;
import com.hzf.api.rpc.api.ApiService;
import com.hzf.common.base.BaseController;
import com.hzf.common.face.*;
import com.hzf.common.face.faceModel.FaceEntity;
import com.hzf.common.util.ByteUtils;
import com.hzf.common.util.HttpUtil;
import com.hzf.common.util.PropertiesFileUtil;
import com.hzf.common.util.RedisUtil;
import com.hzf.common.wxgps.WxGps;
import com.hzf.config.dao.model.ConfigRegion;
import com.hzf.config.dao.model.ConfigRegionExample;
import com.hzf.designer.dao.model.*;
import com.hzf.designer.rpc.api.DneedsDesignerService;
import com.hzf.ucenter.dao.model.*;
import com.hzf.wxplatform.common.WxplatformResult;
import com.hzf.wxplatform.common.WxplatformResultConstant;
import com.hzf.wxplatform.common.utils.UcenterUserUtils;
import com.hzf.wxplatform.common.utils.WxGetGpsUtils;
import io.swagger.annotations.Api;
import net.sf.json.JSONObject;
import org.apache.http.protocol.HTTP;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import sun.security.krb5.Config;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.*;

@Controller
@RequestMapping("/designerUser")
public class DesignerUserController extends BaseController {
    @Autowired
    ApiService apiService;
    private final static String HZF_WX_CLIENT_SESSION_ID = "hzf-upms-client-session-id";
    @RequestMapping(value = "/index")
    public String index(Model model, HttpServletRequest request){
    HttpSession session = request.getSession();
    UcenterUser ucenterUser = new UcenterUser();
    byte[]user =  UcenterUserUtils.getUser(session);
    if(null==user){
        model.addAttribute("data","您的操作属于非法操作，所查看商品或设计师非您账户，请核实。");
        return  thymeleaf("/404");
    }
    ucenterUser = (UcenterUser) ByteUtils.ByteToObject(user);
    if(null==ucenterUser){
        model.addAttribute("data","您的操作属于非法操作，所查看商品或设计师非您账户，请核实。");
        return  thymeleaf("/404");
    }
    System.out.println(ucenterUser.getAuthInfo());

    model.addAttribute("user", ucenterUser);
    return  thymeleaf("/SJS/SJS_Single");
}
    @RequestMapping(value="/designerInfo")
    public String designerInfo(Model model,HttpServletRequest httpServletRequest){

    byte[]user =  UcenterUserUtils.getUser(httpServletRequest.getSession());

    if(null==user){
        model.addAttribute("data","您的操作属于非法操作，所查看商品或设计师非您账户，请核实。");
        return  thymeleaf("/404");
    }
    UcenterUser ucenterUser = new UcenterUser();
    ucenterUser = (UcenterUser) ByteUtils.ByteToObject(user);
    if(null==ucenterUser){
        model.addAttribute("data","您的操作属于非法操作，所查看商品或设计师非您账户，请核实。");
        return  thymeleaf("/404");
    }
    DneedsDesignerExample dneedsDesignerExample = new DneedsDesignerExample();
    dneedsDesignerExample.createCriteria().andUserIdEqualTo(ucenterUser.getId());
     DneedsDesigner dneedsDesigner = apiService.getDesigner(dneedsDesignerExample);
    byte[] address = UcenterUserUtils.getAddress(ucenterUser.getId());
    ConfigRegion configRegion = new ConfigRegion();
    configRegion = (ConfigRegion) ByteUtils.ByteToObject(address);
    ConfigRegionExample configRegionExample  = new ConfigRegionExample();
    long type = 1;
    configRegionExample.createCriteria().andRegionTypeEqualTo(type).andRegionNumberNotEqualTo(configRegion.getRegionNumber());
    List<ConfigRegion> list = apiService.selectRegion(configRegionExample);
    model.addAttribute("configList",list);
    model.addAttribute("configRegion",configRegion);
    if(null!=dneedsDesigner){
        model.addAttribute("dneedsDesigner",dneedsDesigner);
        return "/SJS/SJS_info";
    }

    return  thymeleaf("/SJS/SJS_infoAdd");
}
@RequestMapping(value="/designerSave",method = RequestMethod.POST)
@ResponseBody
public Object designerSave(Model model,HttpServletRequest request,DneedsDesigner dneedsDesigner){
    byte[]user =  UcenterUserUtils.getUser(request.getSession());

    if(null==user){
        model.addAttribute("data","您的操作属于非法操作，所查看商品或设计师非您账户，请核实。");
        return  thymeleaf("/404");
    }
    UcenterUser ucenterUser = new UcenterUser();
    ucenterUser = (UcenterUser) ByteUtils.ByteToObject(user);
    if(null==ucenterUser){
        model.addAttribute("data","您的操作属于非法操作，所查看商品或设计师非您账户，请核实。");
        return  thymeleaf("/404");
    }

   String result= WxGetGpsUtils.lngRequest(request,dneedsDesigner.getLat().toString(),dneedsDesigner.getLng().toString());
    JSONObject  jsonObject = JSONObject.fromObject(result);
    byte[] xbuff = Base64.decodeFast((String) jsonObject.get("x"));
    byte[] ybuff = Base64.decodeFast((String) jsonObject.get("y"));
    System.out.println( );
    System.out.println(new String(ybuff) );

    dneedsDesigner.setLat( Double.valueOf(new String(ybuff)));
    dneedsDesigner.setLng(Double.valueOf(new String(xbuff)));
    GeoHash geoHash = GeoHash.fromCoordinate(dneedsDesigner.getLat(),dneedsDesigner.getLng());
    //4024744861876082L
    long longValue = geoHash.toLong();
    dneedsDesigner.setGeoCode(longValue);
    System.out.println(result);
    dneedsDesigner.setUserId(ucenterUser.getId());
    dneedsDesigner.setOrderState(0);
    dneedsDesigner.setState(0);
    dneedsDesigner.setSales(0);
    int i = apiService.installDesigner(dneedsDesigner);
    if(i!=0){
        return new WxplatformResult(WxplatformResultConstant.SUCCESS, "资料设置成功");
    }else{
        return new WxplatformResult(WxplatformResultConstant.FAILED, "您的资料已存在");

    }
}
    public static double bytes2Double(byte[] arr) {
        long value = 0;
        for (int i = 0; i < 8; i++) {
            value |= ((long) (arr[i] & 0xff)) << (8 * i);
        }
        return Double.longBitsToDouble(value);
    }
    @RequestMapping(value="/updateDesigner",method = RequestMethod.POST)
    @ResponseBody
    public Object updateDesigner(Model model ,DneedsDesigner dneedsDesigner,int updateType,HttpServletRequest request){
        byte[]user =  UcenterUserUtils.getUser(request.getSession());

        if(null==user){
            model.addAttribute("data","您的操作属于非法操作，所查看商品或设计师非您账户，请核实。");
            return  thymeleaf("/404");
        }
        UcenterUser ucenterUser = new UcenterUser();
        ucenterUser = (UcenterUser) ByteUtils.ByteToObject(user);
        if(null==ucenterUser){
            model.addAttribute("data","您的操作属于非法操作，所查看商品或设计师非您账户，请核实。");
            return  thymeleaf("/404");
        }
        if(updateType==0){
            DneedsDesignerExample dneedsDesignerExample = new DneedsDesignerExample();
            dneedsDesignerExample.createCriteria().andUserIdEqualTo(ucenterUser.getId());
            DneedsDesigner dneedsDesigner1 = apiService.getDesigner(dneedsDesignerExample);
            dneedsDesigner1.setOrderState(dneedsDesigner.getOrderState());
            apiService.updateDesigner(dneedsDesigner1);
        }else{
            apiService.updateDesigner(dneedsDesigner);

        }
        return new WxplatformResult(WxplatformResultConstant.SUCCESS, "更改成功");

    }
    @RequestMapping(value = "/anliAdd")
    private String anliAdd(Model model,HttpServletRequest request){
        byte[]user =  UcenterUserUtils.getUser(request.getSession());

        if(null==user){
            model.addAttribute("data","您的操作属于非法操作，所查看商品或设计师非您账户，请核实。");
            return  thymeleaf("/404");
        }
        UcenterUser ucenterUser = new UcenterUser();
        ucenterUser = (UcenterUser) ByteUtils.ByteToObject(user);
        if(null==ucenterUser){
            model.addAttribute("data","您的操作属于非法操作，所查看商品或设计师非您账户，请核实。");
            return  thymeleaf("/404");
        }
        DneedsDesignerCaseExample dneedsDesignerCaseExample = new DneedsDesignerCaseExample();
        dneedsDesignerCaseExample.createCriteria().andUserIdEqualTo(ucenterUser.getId());
        List<DneedsDesignerCase> list =apiService.selectDneedsDesignerCase(dneedsDesignerCaseExample);
        if(null!=list&&0!=list.size()){
            model.addAttribute("list",list);
            return  thymeleaf("/SJS/SJS_AnliZhanShi");
        }else{
            return thymeleaf("/SJS/SJS_AnliAdd");
        }
    }
    @RequestMapping(value = "/anliSave",method = RequestMethod.POST)
    @ResponseBody
    public Object anliSave(DneedsDesignerCase dneedsDesignerCase,Model model,HttpServletRequest servletRequest){
        byte[]user =  UcenterUserUtils.getUser(servletRequest.getSession());

        if(null==user){
            model.addAttribute("data","您的操作属于非法操作，所查看商品或设计师非您账户，请核实。");
            return  thymeleaf("/404");
        }
        UcenterUser ucenterUser = new UcenterUser();
        ucenterUser = (UcenterUser) ByteUtils.ByteToObject(user);
        if(null==ucenterUser){
            model.addAttribute("data","您的操作属于非法操作，所查看商品或设计师非您账户，请核实。");
            return  thymeleaf("/404");
        }
        dneedsDesignerCase.setUserId(ucenterUser.getId());
        apiService.installDneedsDesignerCase(dneedsDesignerCase);
        return new WxplatformResult(WxplatformResultConstant.SUCCESS, "保存成功");
    }
    @RequestMapping(value = "/jujue",method = RequestMethod.POST)
    @ResponseBody
    public Object jujue(int id,Model model,HttpServletRequest servletRequest){
        byte[]user =  UcenterUserUtils.getUser(servletRequest.getSession());

        if(null==user){
            model.addAttribute("data","您的操作属于非法操作，所查看商品或设计师非您账户，请核实。");
            return  thymeleaf("/404");
        }
        UcenterUser ucenterUser = new UcenterUser();
        ucenterUser = (UcenterUser) ByteUtils.ByteToObject(user);
        if(null==ucenterUser){
            model.addAttribute("data","您的操作属于非法操作，所查看商品或设计师非您账户，请核实。");
            return  thymeleaf("/404");
        }
        DneedsDemandExample example = new DneedsDemandExample();
        example.createCriteria().andIdEqualTo(id).andUserIdEqualTo(ucenterUser.getId());
        DneedsDemand dneedsDemand = apiService.getDneedsDemand(example);
        if(null!=dneedsDemand){
            dneedsDemand.setDemandType(5);
            apiService.updateDneedsDemand(dneedsDemand);
            return new WxplatformResult(WxplatformResultConstant.SUCCESS, "已拒绝");
        }else{
            model.addAttribute("data","您的操作属于非法操作，所查看商品或设计师非您账户，请核实。");
            return  thymeleaf("/404");
        }
    }
    @RequestMapping(value = "/jujuewl",method = RequestMethod.POST)
    @ResponseBody
    public Object jujuewl(int id,Model model,HttpServletRequest servletRequest){
        byte[]user =  UcenterUserUtils.getUser(servletRequest.getSession());

        if(null==user){
            model.addAttribute("data","您的操作属于非法操作，所查看商品或设计师非您账户，请核实。");
            return  thymeleaf("/404");
        }
        UcenterUser ucenterUser = new UcenterUser();
        ucenterUser = (UcenterUser) ByteUtils.ByteToObject(user);
        if(null==ucenterUser){
            model.addAttribute("data","您的操作属于非法操作，所查看商品或设计师非您账户，请核实。");
            return  thymeleaf("/404");
        }
        DneedsDemandExample example = new DneedsDemandExample();
        example.createCriteria().andIdEqualTo(id).andUserIdEqualTo(ucenterUser.getId());
        DneedsDemand dneedsDemand = apiService.getDneedsDemand(example);
        if(null!=dneedsDemand){
            dneedsDemand.setDemandType(6);
            apiService.updateDneedsDemand(dneedsDemand);
            return new WxplatformResult(WxplatformResultConstant.SUCCESS, "已拒绝");
        }else{
            model.addAttribute("data","您的操作属于非法操作，所查看商品或设计师非您账户，请核实。");
            return  thymeleaf("/404");
        }
    }
    @RequestMapping(value = "/jiedanIndex")
    private String jiedan(Model model,HttpServletRequest servletRequest){
        return thymeleaf("/SJS/SJS_JieDan");
    }

    @RequestMapping(value = "/sjtSave",method = RequestMethod.POST)
    @ResponseBody
    public Object sjtSave(DneedsDemandSjt dneedsDemandSjt,Model model,HttpServletRequest servletRequest){
        byte[]user =  UcenterUserUtils.getUser(servletRequest.getSession());

        if(null==user){
            model.addAttribute("data","您的操作属于非法操作，所查看商品或设计师非您账户，请核实。");
            return  thymeleaf("/404");
        }
        UcenterUser ucenterUser = new UcenterUser();
        ucenterUser = (UcenterUser) ByteUtils.ByteToObject(user);
        DneedsDemandExample dneedsDemandExample = new DneedsDemandExample();
        dneedsDemandExample.createCriteria().andIdEqualTo(dneedsDemandSjt.getDemandId());
        DneedsDemand dneedsDemand = apiService.getDneedsDemand(dneedsDemandExample);
        DneedsDemandWltExample dneedsDemandWltExample = new DneedsDemandWltExample();
        dneedsDemandWltExample.createCriteria().andDemandIdEqualTo(dneedsDemandSjt.getDemandId());
        DneedsDemandWlt dneedsDemandWlt = apiService.getDneedsDemandWlt(dneedsDemandWltExample);
        if(null!=dneedsDemandWlt){
            dneedsDemand.setDemandType(4);
            apiService.updateDneedsDemand(dneedsDemand);
        }
        DneedsBidExample example  =new DneedsBidExample();
        example.createCriteria().andDemandIdEqualTo(dneedsDemandSjt.getDemandId()).andUserIdEqualTo(ucenterUser.getId());
        DneedsBid dneedsBid = apiService.getDneedsBid(example);
        if(null!=dneedsBid){
            dneedsDemandSjt.setCreatTime(System.currentTimeMillis());
            int i = apiService.installDneedsDemandSjt(dneedsDemandSjt);
            if(i<1){
                return new WxplatformResult(WxplatformResultConstant.SUCCESS, "已经添加过设计图，如需修改请在页面点击修改");
            }
            return new WxplatformResult(WxplatformResultConstant.SUCCESS, "保存成功");

        }
        model.addAttribute("data","您的操作属于非法操作，所查看商品或设计师非您账户，请核实。");
        return  thymeleaf("/404");    }
    @RequestMapping(value = "/wltSave",method = RequestMethod.POST)
    @ResponseBody
    public Object wltSave(DneedsDemandWlt dneedsDemandSjt,Model model,HttpServletRequest servletRequest){
        byte[]user =  UcenterUserUtils.getUser(servletRequest.getSession());

        if(null==user){
            model.addAttribute("data","您的操作属于非法操作，所查看商品或设计师非您账户，请核实。");
            return  thymeleaf("/404");
        }
        UcenterUser ucenterUser = new UcenterUser();
        ucenterUser = (UcenterUser) ByteUtils.ByteToObject(user);
        DneedsDemandExample dneedsDemandExample = new DneedsDemandExample();
        dneedsDemandExample.createCriteria().andIdEqualTo(dneedsDemandSjt.getDemandId());
        DneedsDemand dneedsDemand = apiService.getDneedsDemand(dneedsDemandExample);
        DneedsDemandSjtExample dneedsDemandSjtExample = new DneedsDemandSjtExample();
        dneedsDemandSjtExample.createCriteria().andDemandIdEqualTo(dneedsDemandSjt.getDemandId());
        DneedsDemandSjt dneedsDemandSjt1= apiService.getDneedsDemandSjt(dneedsDemandSjtExample);
        if(null!=dneedsDemandSjt1){
            dneedsDemand.setDemandType(4);
            apiService.updateDneedsDemand(dneedsDemand);
        }
        DneedsBidExample example  =new DneedsBidExample();
        example.createCriteria().andDemandIdEqualTo(dneedsDemandSjt.getDemandId()).andUserIdEqualTo(ucenterUser.getId());
        DneedsBid dneedsBid = apiService.getDneedsBid(example);
        if(null!=dneedsBid){
            int i = apiService.installDneedsDemandWlt(dneedsDemandSjt);
            if(i<1){
                return new WxplatformResult(WxplatformResultConstant.SUCCESS, "已经添加过设计图，如需修改请在页面点击修改");
            }
            return new WxplatformResult(WxplatformResultConstant.SUCCESS, "保存成功");

        }
        model.addAttribute("data","您的操作属于非法操作，所查看商品或设计师非您账户，请核实。");
        return  thymeleaf("/404");    }
    @RequestMapping(value = "/sjtUpdate",method = RequestMethod.POST)
    @ResponseBody
    public Object sjtUpdate(DneedsDemandSjt dneedsDemandSjt,Model model,HttpServletRequest servletRequest){
        byte[]user =  UcenterUserUtils.getUser(servletRequest.getSession());

        if(null==user){
            model.addAttribute("data","您的操作属于非法操作，所查看商品或设计师非您账户，请核实。");
            return  thymeleaf("/404");
        }
        UcenterUser ucenterUser = new UcenterUser();
        ucenterUser = (UcenterUser) ByteUtils.ByteToObject(user);
        DneedsDemandExample dneedsDemandExample = new DneedsDemandExample();
        dneedsDemandExample.createCriteria().andIdEqualTo(dneedsDemandSjt.getDemandId());
        DneedsDemand dneedsDemand = apiService.getDneedsDemand(dneedsDemandExample);
        if(null!=dneedsDemand){
            dneedsDemand.setDemandType(4);
            apiService.updateDneedsDemand(dneedsDemand);
            DneedsBidExample example  =new DneedsBidExample();
            example.createCriteria().andDemandIdEqualTo(dneedsDemandSjt.getDemandId()).andUserIdEqualTo(ucenterUser.getId());
            DneedsBid dneedsBid = apiService.getDneedsBid(example);
            if(null!=dneedsBid){
                apiService.updateDneedsDemandSjt(dneedsDemandSjt);
                return new WxplatformResult(WxplatformResultConstant.SUCCESS, "保存成功");

            }
        }

        model.addAttribute("data","您的操作属于非法操作，所查看商品或设计师非您账户，请核实。");
        return  thymeleaf("/404");    }
    @RequestMapping(value = "/wltUpdate",method = RequestMethod.POST)
    @ResponseBody
    public Object wltUpdate(DneedsDemandWlt dneedsDemandSjt,Model model,HttpServletRequest servletRequest){
        byte[]user =  UcenterUserUtils.getUser(servletRequest.getSession());

        if(null==user){
            model.addAttribute("data","您的操作属于非法操作，所查看商品或设计师非您账户，请核实。");
            return  thymeleaf("/404");
        }
        UcenterUser ucenterUser = new UcenterUser();
        ucenterUser = (UcenterUser) ByteUtils.ByteToObject(user);
        DneedsDemandExample dneedsDemandExample = new DneedsDemandExample();
        dneedsDemandExample.createCriteria().andIdEqualTo(dneedsDemandSjt.getDemandId());
        DneedsDemand dneedsDemand = apiService.getDneedsDemand(dneedsDemandExample);
        if(null!=dneedsDemand){
            dneedsDemand.setDemandType(4);
            apiService.updateDneedsDemand(dneedsDemand);
            DneedsBidExample example  =new DneedsBidExample();
            example.createCriteria().andDemandIdEqualTo(dneedsDemandSjt.getDemandId()).andUserIdEqualTo(ucenterUser.getId());
            DneedsBid dneedsBid = apiService.getDneedsBid(example);
            if(null!=dneedsBid){
                apiService.updateDneedsDemandWlt(dneedsDemandSjt);
                return new WxplatformResult(WxplatformResultConstant.SUCCESS, "保存成功");

            }
        }

        model.addAttribute("data","您的操作属于非法操作，所查看商品或设计师非您账户，请核实。");
        return  thymeleaf("/404");    }

    /**
     * 队列比较
     *
     * @param <T>
     * @param a
     * @param b
     * @return
     */
    public static <T extends Comparable<T>> boolean compare(List<T> a, List<T> b) {
        if (a.size() != b.size())
            return false;
        Collections.sort(a);
        Collections.sort(b);
        for (int i = 0; i < a.size(); i++) {
            if (!a.get(i).equals(b.get(i)))
                return false;
        }
        return true;
    }
}
